package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.ChatRecordAdapter;
import com.example.capsule_chat0322.bean.ChatRecordBean;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.ChatRecordItemOnClickInterface;
import com.example.capsule_chat0322.request.MessagesRequest;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/15 23:09
 */
public class RecentChatRecordActivity extends AppCompatActivity {

    private List<ChatRecordBean> chatRecordBeanList;
    private RecyclerView recent_chat_record_recycler_view;
    private ChatRecordAdapter chatRecordAdapter;
    private TextView sender_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recent_chat_record);
        initView();//初始化UI控件
    }

    private void initView() {

        recent_chat_record_recycler_view = findViewById(R.id.recent_chat_record_recycler_view);
        sender_name = findViewById(R.id.sender_name);
        String senderName = getIntent().getStringExtra("senderName");
        String senderId = getIntent().getStringExtra("senderId");
        sender_name.setText(senderName);
        doGetRecentMessage(senderId);

    }

    private void doGetRecentMessage(String senderId) {

        MessageBean messageBean = new MessageBean();
        messageBean.setField(GlobalAssistMap.getObject("field").toString());

        messageBean.setSenderId(senderId);
        MessagesRequest.getRecentMessage(messageBean, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {

                chatRecordBeanList = (List<ChatRecordBean>) message.obj;
                LinearLayoutManager layoutManager = new LinearLayoutManager(RecentChatRecordActivity.this);
                recent_chat_record_recycler_view.setLayoutManager(layoutManager);
                chatRecordAdapter = new ChatRecordAdapter(chatRecordBeanList);
                adapterAddListener(chatRecordAdapter);
                recent_chat_record_recycler_view.setAdapter(chatRecordAdapter);
                return true;
            }
        }));


    }

    public void adapterAddListener(ChatRecordAdapter chatRecordAdapter) {

        if (chatRecordAdapter == null) {
            return;
        }

        chatRecordAdapter.setFunctionItemOnClickInterface(new ChatRecordItemOnClickInterface() {

            @Override
            public void onItemClick(RecyclerView parent, View view, String roomName, String senderId, String senderName, String content, String sendTime) {
                UserInfoBean userInfoBean = (UserInfoBean) GlobalAssistMap.getObject("userInfo");
                if (String.valueOf(userInfoBean.getId()).equals(senderId)) {
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("roomName", roomName);
                intent.putExtra("senderId", senderId);
                intent.putExtra("senderName", senderName);
                intent.putExtra("content", content);
                intent.putExtra("sendTime", sendTime.substring(0, sendTime.lastIndexOf(".")));
                intent.setClass(RecentChatRecordActivity.this, OneChatRecordInfoActivity.class);
                startActivity(intent);
            }
        });
    }


}
